"""
为用户 ID 1041 创建 StudentProfile
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from dotenv import load_dotenv
load_dotenv()

from app import create_app
from app.extensions import db
from app.models.user import User, StudentProfile, TeacherProfile


def fix_user():
    """为指定用户创建profile"""
    app = create_app('development')
    
    with app.app_context():
        print("=" * 60)
        print("查找用户 ID: 1041")
        print("=" * 60)
        
        user = User.query.get(1041)
        
        if not user:
            print("❌ 用户不存在")
            return
        
        print(f"\n✅ 用户信息：")
        print(f"ID: {user.id}")
        print(f"用户名: {user.username}")
        print(f"姓名: {user.last_name}{user.first_name}")
        print(f"角色: {user.role}")
        print(f"学院: {user.college}")
        print(f"系部: {user.department}")
        
        # 检查是否已有profile
        if user.role == 'student':
            if user.student_profile:
                print(f"\n✅ StudentProfile 已存在 (ID: {user.student_profile.id})")
                print("无需创建")
                return
            else:
                print(f"\n⚠️  StudentProfile 不存在，开始创建...")
                profile = StudentProfile(
                    user_id=user.id,
                    student_id=user.username,
                    major=user.department,
                    grade_level=1
                )
                db.session.add(profile)
                db.session.commit()
                
                print(f"✅ StudentProfile 创建成功！")
                print(f"  Profile ID: {profile.id}")
                print(f"  Student ID: {profile.student_id}")
                print(f"  Major: {profile.major}")
                print(f"  Grade Level: {profile.grade_level}")
                print(f"\n🎉 现在可以为该学生添加课程了！")
        
        elif user.role == 'teacher':
            if user.teacher_profile:
                print(f"\n✅ TeacherProfile 已存在 (ID: {user.teacher_profile.id})")
                print("无需创建")
                return
            else:
                print(f"\n⚠️  TeacherProfile 不存在，开始创建...")
                profile = TeacherProfile(
                    user_id=user.id,
                    employee_id=user.username,
                    department=user.department,
                    title='讲师'
                )
                db.session.add(profile)
                db.session.commit()
                
                print(f"✅ TeacherProfile 创建成功！")
                print(f"  Profile ID: {profile.id}")
                print(f"  Employee ID: {profile.employee_id}")
                print(f"  Department: {profile.department}")
        
        print("\n" + "=" * 60)


if __name__ == '__main__':
    fix_user()


